package com.zftlive.android.library.tools.sharesdk;

import android.os.Handler;
import android.os.Message;

import com.zftlive.android.library.MApplication;
import com.zftlive.android.library.tools.ToolAlert;
import com.zftlive.android.library.tools.ToolLog;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;

/**
 * 平台分享行为监听器
 *
 * @author 曾繁添
 * @version 1.0
 */
public class SharePlatformActionListener implements PlatformActionListener {

    private Handler mShareHandle;
    public final static int ACTION_CANCEL = -1;
    public final static int ACTION_COMPLETE = 0;
    public final static int ACTION_ERROR = 1;
    private static final String TAG = SharePlatformActionListener.class.getSimpleName();

    public SharePlatformActionListener(){
        mShareHandle = new MyShareHandler();
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        //组装消息
        Message msg = mShareHandle.obtainMessage();
        msg.what = ACTION_COMPLETE;
        msg.obj = "分享成功"+i;
        mShareHandle.sendMessage(msg);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        //组装消息
        Message msg = mShareHandle.obtainMessage();
        msg.what = ACTION_ERROR;
        msg.obj = "分享"+platform.getName()+"失败，原因："+throwable.getMessage();
        mShareHandle.sendMessage(msg);
    }

    @Override
    public void onCancel(Platform platform, int i) {
        //组装消息
        Message msg = mShareHandle.obtainMessage();
        msg.what = ACTION_CANCEL;
        msg.obj = "取消了"+platform.getName()+"分享操作"+i;
        mShareHandle.sendMessage(msg);

    }

    /**
     * 消息处理Handle
     */
    private class MyShareHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ToolLog.e(TAG, msg.obj + "");
            switch (msg.what){
                case ACTION_ERROR:
                    ToolAlert.showCenterText(MApplication.gainContext(), msg.obj + "");
                    break;
                case ACTION_COMPLETE:
                    ToolAlert.showCenterText(MApplication.gainContext(), msg.obj + "");
                    break;
                case ACTION_CANCEL:
                    ToolAlert.showCenterText(MApplication.gainContext(), msg.obj + "");
                    break;
                default:
                    break;
            }
        }
    }
}
